home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 551-575 / disk_553 / 24bittools / pro2iff / main.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  3KB  |  162 lines

  1. /* Pro2IFF.c - converts Amiga 3-D Professional RGB files to 24-bit
  2.    ILBM format - djh */
  3.    
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include "iff.h"
  7.  
  8. void *GfxBase;
  9.  
  10. #define PROIDLEN 9
  11. #define STD_METHOD 1
  12. #define RGBSIZE 3 /* Aztec pads sizeof(rgb) to 4! */
  13.  
  14. typedef struct {
  15.   /*char identifier[9];*/
  16.   short width,height,vxoff,vyoff,
  17.         vwidth,vheight;
  18.   long dflags,method;
  19. } ProRGBHeader;
  20.  
  21. typedef struct {
  22.   unsigned char r,g,b;
  23. } rgb;
  24.  
  25. SafeRead2(FILE *fp,char *buf,int len)
  26. {
  27.   if (fread(buf,1,len,fp)!=len) {
  28.     puts("\nError while reading file.\n");
  29.     return(TRUE);
  30.   }
  31.  
  32.   return(FALSE);
  33. }
  34.  
  35.   void
  36. MyInitBitMap(struct BitMap *bm,int pl,int w,int h)
  37. {
  38.   /* Looks, smells, tastes like InitBitMap, but supports 24-bit. Note
  39.      that the 24-bit BitMap structures used here are always embedded
  40.      within a PicMap, which lengthens it by 16 extra planes. */
  41.  
  42.   memset(bm,0,sizeof(*bm));
  43.  
  44.   bm->Depth=pl;
  45.   bm->BytesPerRow=w/8;
  46.   bm->Rows=h;
  47. }
  48.  
  49.   void
  50. split_row24(struct PicMap *picmap,int y,UBYTE *buf)
  51. {
  52.   ULONG offset,color;
  53.   UWORD index,pixels,plane;
  54.   short bit;
  55.   UBYTE value;
  56.  
  57.   pixels=picmap->BitMap.BytesPerRow*8;
  58.   offset=picmap->BitMap.BytesPerRow*y;
  59.  
  60.   bit=7;
  61.  
  62.   /* Read 3-byte rgb line from image and break apart into
  63.      separate bitplanes. Yes, this is relatively slow. */
  64.  
  65.   for (index=0;index<pixels;index++) {
  66.     color  =  *buf++;        /* red */
  67.     color |= (*buf++ << 8);    /* green */
  68.     color |= (*buf++ << 16);    /* blue */
  69.  
  70.     value=1<<bit;
  71.  
  72.     for (plane=0;plane<24;plane++)
  73.       if (color & (1<<plane))
  74.         picmap->BitMap.Planes[plane][offset] |= value;
  75.  
  76.     if (--bit < 0) {
  77.       bit=7;
  78.       offset++;
  79.     }
  80.   }
  81. }
  82.  
  83. Pro2ILBM24(FILE *fp,struct PicMap *dst)
  84. {
  85.   ProRGBHeader pro_hdr;
  86.   static char pro_id[PROIDLEN+1];
  87.   UBYTE buf[1024*3];
  88.   UWORD w,h;
  89.   int linesize,i;
  90.  
  91.   memset(dst,0,sizeof(*dst));
  92.  
  93.   if (SafeRead2(fp,pro_id,PROIDLEN)) return(DISK_ERR);
  94.   if (SafeRead2(fp,(char *)&pro_hdr,sizeof(pro_hdr))) return(DISK_ERR);
  95.  
  96.   if (pro_hdr.method!=STD_METHOD) {
  97.     printf("\nError: storage method %d not recognized!\n",pro_hdr.method);
  98.     return(DISK_ERR);
  99.   }
  100.  
  101.   w=pro_hdr.width,h=pro_hdr.height,linesize=w*3;
  102.   printf("Converting [Width=%d,Height=%d]... ",w,h);
  103.  
  104.   MyInitBitMap(&dst->BitMap,24,(long)w,(long)h);
  105.  
  106.   /* note that we're not allocating CHIP_MEM here! */
  107.  
  108.   for (i=0;i<24;i++)
  109.     if (!(dst->BitMap.Planes[i]=AllocMem(RASSIZE(w,h),MEMF_PUBLIC|MEMF_CLEAR))) {
  110.       FreeBitMap(&dst->BitMap);
  111.       return(OUT_OF_MEM);
  112.     }
  113.  
  114.   linesize=pro_hdr.width*RGBSIZE;
  115.  
  116.   for (i=0;i<pro_hdr.height;i++) {
  117.     if (SafeRead2(fp,(char *)buf,linesize)) return(DISK_ERR);
  118.     split_row24(dst,i,buf);
  119.   }
  120.  
  121.   return(0);
  122. }
  123.  
  124. main(argc,argv)
  125. int argc;
  126. char *argv[];
  127. {
  128.   struct PicMap dstmap;
  129.   FILE *fp=NULL;
  130.  
  131.   puts("Pro2IFF: 3-D Professional to 24-bit Windows converter");
  132.   puts("         Copyright (c) 1991 Dallas J. Hodgson\n");
  133.  
  134.   if (argc<3) {  
  135.     puts("Usage: Pro2IFF <infile> <outfile>");
  136.     goto cleanup;
  137.   }
  138.  
  139.   if (!(fp=fopen(argv[1],"r"))) {
  140.     printf("Error: couldn't open ProRGB file %s for reading.\n",argv[1]);
  141.     goto cleanup;
  142.   }
  143.  
  144.   if (Pro2ILBM24(fp,&dstmap)) {
  145.     puts("not enough memory!");
  146.     goto cleanup;
  147.   }
  148.  
  149.   if (WriteILBM(argv[2],&dstmap)) {
  150.     printf("Couldn't write file %s.\n",argv[2]);
  151.     goto cleanup;
  152.   }
  153.  
  154.   puts("Done!");
  155.   
  156. cleanup:
  157.   FreeBitMap(&dstmap.BitMap);
  158.   if (fp) fclose(fp);
  159.  
  160.   return(0);
  161. }
  162.